package com.fmdj.mps.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.fmdj.common.util.CommonResult;
import com.fmdj.mps.controller.form.*;
import com.fmdj.mps.service.DriverPositionService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.sql.Driver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/driver/position")
public class DriverPositionController {

    @Autowired
    private DriverPositionService driverPositionService;

    @PostMapping("/updatePositionCache")
    @Operation(summary = "更新司机位置到redis中")
    public CommonResult updatePositionCache(@Valid @RequestBody UpdateLocationCacheForm form) {
        driverPositionService.updatePositionCache(BeanUtil.beanToMap(form));
        return CommonResult.ok();
    }

    @PostMapping("/removePositionCache")
    @Operation(summary = "删除司机位置缓存")
    public CommonResult removePositionCache(@Valid @RequestBody RemoveLocationCacheForm form) {
        driverPositionService.removePositionCache(form.getDriverId());
        return CommonResult.ok();
    }

    @PostMapping("/selectBefittingDriverAboutOrder")
    @Operation(summary = "查询附近符合条件的司机")
    public CommonResult selectBefittingDriverAboutOrder(@Valid @RequestBody SearchBefittingDriverAboutOrderForm form) {
        double endPlaceLatitude = Double.parseDouble(form.getEndPlaceLatitude());
        double endPlaceLongitude = Double.parseDouble(form.getEndPlaceLongitude());
        double mileage = Double.parseDouble(form.getMileage());
        double  startPlaceLatitude = Double.parseDouble(form.getStartPlaceLatitude());
        double startPlaceLongitude = Double.parseDouble(form.getStartPlaceLongitude());

        ArrayList arrayList = driverPositionService.selectBefittingDriverAboutOrder(startPlaceLatitude, startPlaceLongitude, endPlaceLatitude, endPlaceLongitude, mileage);

            return CommonResult.ok().put(CommonResult.RETURN_RESULT, arrayList);
    }

    @PostMapping("/updateOrderPositionCache")
    @Operation(summary = "更新订单位置缓存")
    public CommonResult updateOrderPositionCache(@Valid @RequestBody UpdateOrderLocationCacheForm form) {
        Map<String, Object> map = BeanUtil.beanToMap(form);
        driverPositionService.updateOrderPositionCache(map);
        return CommonResult.ok();  //service层没有返回值 这里返回ok即可
    }

    @PostMapping("/searchOrderPositionCache")
    @Operation(summary = "查询订单位置缓存")
    public CommonResult searchOrderPositionCache(@Valid @RequestBody SearchOrderLocationCacheForm form) {
        HashMap hashMap = driverPositionService.searchOrderPositionCache(form.getOrderId());
        return CommonResult.ok().put(CommonResult.RETURN_RESULT, hashMap);
    }

}
